﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Manage.Model;
using Newtonsoft.Json;

namespace Manage.Basic
{
    public partial class FrmDeviceMgr : Form
    {
        public FrmDeviceMgr()
        {
            InitializeComponent();
        }
        private int Sid;
        private string SDeviceIP;
        private int SNumber;
        private string SRemark;
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                DeviceInfoModel dim = new DeviceInfoModel();
                dim.DeviceIP = tbxIPAddress.Text.Trim();
                dim.Number = Convert.ToInt32(tbxMNumber.Text.Trim());
                dim.Remark = tbxRemark.Text.Trim();
                RetModel rm = Share.PostData("51API/UserManage/DeviceAdd", dc, JsonConvert.SerializeObject(dim));
                if (rm.Success)
                {
                    MessageBox.Show("设备添加完成!");
                    tbxIPAddress.Text = "";
                    tbxMNumber.Text = "";
                    tbxRemark.Text = "";
                    return;
                }
                else
                {
                    MessageBox.Show("设备添加失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Sid.ToString());
                RetModel rm = Share.PostData("51API/UserManage/DeviceDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除成功！");
                    tbxIPAddress.Text = "";
                    tbxMNumber.Text = "";
                    tbxRemark.Text = "";
                    return;
                }
                else
                {
                    MessageBox.Show("删除失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel rm = Share.PostData("51API/UserManage/GetDeviceList", dc, null);
                if (rm.Success)
                {
                    DeviceInfoModel[] dim = JsonConvert.DeserializeObject<DeviceInfoModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("ip地址");
                    dt.Columns.Add("分机号");
                    dt.Columns.Add("备注");
                    for (int i = 0; i < dim.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = dim[i].id;
                        dr["ip地址"] = dim[i].DeviceIP;
                        dr["分机号"] = dim[i].Number;
                        dr["备注"] = dim[i].Remark;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Sid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
                SDeviceIP = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["ip地址"]).ToString();
                SNumber = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["分机号"]));
                SRemark = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["备注"]).ToString();
                tbxIPAddress.Text = SDeviceIP;
                tbxMNumber.Text = SNumber.ToString();
                tbxRemark.Text = SRemark;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
